package com.ruoyi.system.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.mapper.SysRoleMenuMapper;
import com.ruoyi.system.service.IRoleService;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 角色信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/roles")
public class RoleController extends BaseController
{
    @Autowired
    private IRoleService roleService;


    @RequiresPermissions("system:role:list")
    @GetMapping("/list")
    public TableDataInfo list(SysRole role)
    {
        startPage();
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(role.getRoleName()),SysRole::getRoleName, role.getRoleName());
        wrapper.eq(StringUtils.isNotBlank(role.getStatus()),SysRole::getStatus,role.getStatus());
        wrapper.like(StringUtils.isNotBlank(role.getRoleKey()),SysRole::getRoleKey,role.getRoleKey());
        if(role.getParams().size()>0){
            wrapper.between(role.getParams().get("beginTime")!=null && role.getParams().get("endTime")!=null,SysRole::getCreateTime,role.getParams().get("beginTime"),role.getParams().get("endTime"));
        }
        List<SysRole> list = roleService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 新增角色
     */
    @RequiresPermissions("system:role:add")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRole role)
    {
        role.setCreateTime(new Date());
        role.setCreateBy(SecurityUtils.getUsername());
        return toAjax(roleService.saveAll(role));
    }

    /**
     * 根据角色编号获取详细信息
     */
    @RequiresPermissions("system:role:query")
    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable Long roleId)
    {
        return success(roleService.getById(roleId));
    }

    /**
     * 修改保存角色
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRole role)
    {
        role.setUpdateTime(new Date());
        role.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(roleService.updateRole(role));
    }


}
